package guoanmaker.operator.business.scheduled;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import guoanmaker.operator.business.model.OperatorCustomers;
import guoanmaker.operator.business.model.OperatorMailConfig;
import guoanmaker.operator.business.model.OperatorMessage;
import guoanmaker.operator.business.model.OperatorSendMesToCus;
import guoanmaker.operator.business.model.OperatorUserSignInActive;
import guoanmaker.operator.business.model.PerviderDictionary;
import guoanmaker.operator.business.repository.OperatorMailConfigRepository;
import guoanmaker.operator.business.repository.OperatorSendMesToCusRepositoy;
import guoanmaker.operator.business.repository.OperatorUserSignInActiveRepository;
import guoanmaker.operator.business.repository.OperatorUserSignInRepository;
import guoanmaker.operator.business.repository.ProviderDictionaryRepository;
import guoanmaker.operator.business.service.OperatorCustomersServiceIntegerface;
import guoanmaker.operator.business.service.OperatorMessageServiceIntegerface;
import guoanmaker.operator.business.service.OperatorSendCodeService;
import guoanmaker.operator.business.service.OperatorSendMesToCusServiceIntegerface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.util.JiguangPush;
import guoanmaker.operator.helper.common.util.Main;
import guoanmaker.operator.helper.common.util.StringUtil;

@Component
public class MessageSendScheduled {
	@Autowired
	private OperatorMessageServiceIntegerface operatorMessageServiceIntegerface;
	@Autowired
	private OperatorCustomersServiceIntegerface operatorCustomersServiceIntegerface;
	@Autowired
	private OperatorSendMesToCusServiceIntegerface operatorSendMesToCusServiceIntegerface;
	@Autowired
	private OperatorSendCodeService operatorSendCodeService;
	@Autowired
	private  ProviderDictionaryRepository providerDictionaryRepository;
	@Autowired
	private OperatorMailConfigRepository operatorMailConfigRepository;
	@Autowired
	private OperatorUserSignInActiveRepository operatorUserSignInActiveRepository;
	@Autowired
	private OperatorUserSignInRepository operatorUserSignInRepository;
	
	@Scheduled(fixedRate = 50000)
	public AjaxJson selectCustomersDataSend() {
		Date date=new Date();
		long mow=date.getTime();
		List<OperatorMessage> messages=operatorMessageServiceIntegerface.selectOperatorMessageList();
		for(OperatorMessage om:messages){
			long send=om.getSendTime().getTime();
			if(mow>=send){
				om.setSendStatus("3");
				List<OperatorCustomers> ocs=operatorCustomersServiceIntegerface.selectCustomers(om.getId());
				
				String[] sends=StringUtil.StrList(om.getSendWays());
				 for(int i=0;i<sends.length;i++){
					 if(sends[i].equals("13")){//站内信
						 for(OperatorCustomers oc:ocs){
							 OperatorSendMesToCus od=new OperatorSendMesToCus();
							 od.setEmail(oc.getEmail());
							 od.setPhone(oc.getPhone());
							 od.setSendWay("13");
							 od.setMessId(oc.getMessId());
							 od.setPeopleType(oc.getPeopleType());
							 od.setMesContent(om.getMesContent());
							 od.setMesTitle(om.getTitle());
							 od.setSendTime(om.getSendTime());
							 od.setSendStatus("1");//1  未读  2已读
							 PerviderDictionary perviderDictionary= providerDictionaryRepository.findOne(om.getMesType());
							 od.setMesType(perviderDictionary.getDictName());
							 operatorSendMesToCusServiceIntegerface.saveOperatorSendMesToCus(od);
						 }
						 
						 
						 }
					 if(sends[i].equals("15")){//push推送
						 for(OperatorCustomers oc:ocs){
							 OperatorSendMesToCus od=new OperatorSendMesToCus();
							 od.setEmail(oc.getEmail());
							 od.setPhone(oc.getPhone());
							 od.setSendWay("15");
							 od.setMessId(oc.getMessId());
							 od.setPeopleType(oc.getPeopleType());
							 od.setMesContent(om.getMesContent());
							 od.setMesTitle(om.getTitle());
							 od.setSendTime(om.getSendTime());
							 od.setSendStatus("1");//1  未读  2已读
							 OperatorSendMesToCus sendcus= operatorSendMesToCusServiceIntegerface.saveOperatorSendMesToCus(od);
							 JiguangPush jiguangPush=new JiguangPush();
							 jiguangPush.jiguangPush(oc.getPhone(),om.getMesContent(),om.getTitle(),sendcus.getId());
						 }
						 
						 
						 }
					 if(sends[i].equals("14")){//短信
						 for(OperatorCustomers oc:ocs){
							 OperatorSendMesToCus od=new OperatorSendMesToCus();
							 od.setEmail(oc.getEmail());
							 od.setPhone(oc.getPhone());
							 od.setSendWay("14");
							 od.setMessId(oc.getMessId());
							 od.setPeopleType(oc.getPeopleType());
							 od.setSendStatus("1");//1  未读  2已读
							 operatorSendMesToCusServiceIntegerface.saveOperatorSendMesToCus(od);
							 try {
								operatorSendCodeService.sendMessage(oc.getPhone(), om.getMesContent());
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								System.out.println("短信消息发送失败");
							}
						 }
						 
						 
						 }
					 if(sends[i].equals("16")){//邮件
						 for(OperatorCustomers oc:ocs){
							 OperatorSendMesToCus od=new OperatorSendMesToCus();
							 if(null!=oc.getEmail()){
							 od.setEmail(oc.getEmail());
							 od.setPhone(oc.getPhone());
							 od.setSendWay("16");
							 od.setMessId(oc.getMessId());
							 od.setPeopleType(oc.getPeopleType());
							 od.setMesContent(om.getMesContent());
							 od.setMesTitle(om.getTitle());
							 od.setSendTime(om.getSendTime());
							 od.setSendStatus("1");//1  未读  2已读
							 List<OperatorMailConfig> operatorMailConfigs= operatorMailConfigRepository.findAll();
							OperatorMailConfig operatorMailConfig=operatorMailConfigs.get(0);
							Main main =new Main();
							
							try {
								main.mail(operatorMailConfig.getAccount(),operatorMailConfig.getPassword(),oc.getEmail(),om.getTitle(),om.getMesContent());
								operatorSendMesToCusServiceIntegerface.saveOperatorSendMesToCus(od);
							} catch (Exception e) {
								e.printStackTrace();
								// TODO Auto-generated catch block
								
							}
						 }
							 
						 }
						 
						 
						 }
					 }
				
				 om.setSendStatus("2");
					operatorMessageServiceIntegerface.updateOperatorMessage(om);
			}
			
		}
		return null;
	}
	@Scheduled(fixedRate = 100000)
	public AjaxJson selectaltersignInStatus() {
		Date date=new Date();
		long mow=date.getTime();
		List<OperatorUserSignInActive> operatorUserSignInActivelist=operatorUserSignInActiveRepository.selectByOperatorUserSignInActive(0);
		
		if(operatorUserSignInActivelist.size()>0){
			for(OperatorUserSignInActive signActive:operatorUserSignInActivelist){
				long start=signActive.getSignInCreateTime().getTime();
				if(start<=mow){
					signActive.setSignInStatus(1);
					operatorUserSignInActiveRepository.save(signActive);
				}
			}
		}
			List<OperatorUserSignInActive> operatorUserSignInActivelistIng=operatorUserSignInActiveRepository.selectByOperatorUserSignInActive(1);
			if(operatorUserSignInActivelistIng.size()>0){
				long end=operatorUserSignInActivelistIng.get(0).getSignInEndTime().getTime();
				if(end<=mow){
					operatorUserSignInActivelistIng.get(0).setSignInStatus(2);
					operatorUserSignInActiveRepository.save(operatorUserSignInActivelistIng.get(0));
					
				}
			}
		
		
		return null;
		
	}
	
	
}
